package project_model;

/**
 * AdapterTest
 *
 * @author Syzer
 * @Description 适配器模式
 * @Date 2021/7/9 11:26
 */
public class AdapterTest {

    private interface Target {
        void adapter();
    }

    private static class Adaptee {
        protected void swap() {
            System.out.println("默认交换");
        }
    }

    // 类适配器
    private static class ClassAdapter extends Adaptee implements Target {
        @Override
        public void adapter() {
            swap();
        }
    }

    // 对象适配器
    private static class ObjectAdapter implements Target {
        private Adaptee adaptee;

        public ObjectAdapter(Adaptee adaptee) {
            this.adaptee = adaptee;
        }

        @Override
        public void adapter() {
            adaptee.swap();
        }
    }

    public static void main(String[] args) {
        ClassAdapter classAdapter = new ClassAdapter();
        classAdapter.adapter();

        ObjectAdapter objectAdapter = new ObjectAdapter(new Adaptee());
        objectAdapter.adapter();
    }
}
